import {deleteAllCart, getCartByUsername, updateCart, deleteItemFromCart, addItemToCart} from "../../../src/api/api";
let nowUsername= sessionStorage.getItem("nowUsername");
export default {
  data(){
    return{
      chooseItem:0,
      totalPrice: 260,
      itemId:"",
      cartList:[

      ]
    }
  },
  mounted() {
      this.getCartList();
      // location.reload();
  },
  methods:{
    getCartList(){
      getCartByUsername(nowUsername).then((res) =>{
        this.cartList = res.data.data;
        for (var i = 0;i < res.data.data.length;i++){
          this.cartList[i]['productId'] = res.data.data[i].productId;
          this.cartList[i]['categoryId'] = res.data.data[i].categoryId;
        }
      })
    },
    //点击删除键选择的商品
    chooseItem(itemId){
      this.itemId = itemId;
    },
    //点击确认删除后
    removeItem(){
      deleteItemFromCart(nowUsername,this.itemId).then((res)=>{
        this.getCartList();
      })
    },
    //更新购物车数量
    updateCart(itemId,quantity){
      let param = new FormData;
      param.append("quantity", quantity);
      addItemToCart(nowUsername,itemId,param).then( (res) => {
        this.getCartList();
      }).catch((res) => {
        alert("错误：" + res);
      });
    },
    //清空购物车
    clearCart(){
      deleteAllCart(nowUsername).then((res)=>{
          this.getCartList();
      })
    }
  }
}
